<?php
namespace Lsf\Plugin;

/**
 * 定时任务插件类
 * @author lichenkai@okay.cn
 * $Id: Crontab.php 2020-03-14 14:07:07 lichenkai $
 */

class Crontab
{
    /**
     * 时间匹配
     * @param  string  $timeExpression
     * @return bool
     */
    public function timeMatch($timeExpression){
        // second 00-59 | min 00-59 | hour 0-23 | day 1-31 | month 1-12 | week 0-6
        $timeArr = explode('|', date('s|i|G|j|n|w', time()));
        $timeExpressionArr = explode(' ', $timeExpression);
        $ready = 0;
        // 对应上面的时间数组
        foreach(['second', 'min', 'hour', 'day', 'month', 'week'] as $key => $field){
            if($timeExpressionArr[$key] == '*' || $timeArr[$key] == $timeExpressionArr[$key]){
                $ready += 1;
            }
        }
        return 6 === $ready ? TRUE : FALSE;
    }

}